/*
//半全场
{
  context : [{cat:'半全场', odd:0.8, beton:'主/和', result:'1:0/3:2'}],
  amount : 1000
}
//波胆
{
  context : [{cat:'波胆', odd:0.8, beton:'3:2', result:'3:2'}],
  amount : 1000
}
//入球数
{
  context : [{cat:'入球数', odd:0.8, beton:'5', result:'3:2'}],
  amount : 1000
}
//独赢
{
  context : [{cat:'独赢', odd:0.8, beton:'主', result:'3:2'}],
  amount : 1000
}
//让球
{
  context : [{cat:'让球', odd:0.8, beton:'主', attach:'0.5/1', attach2:'主让客', result:'3:2'}],
  amount : 1000
}
//大小
{
  context : [{cat:'大小', odd:0.8, beton:'大', attach:'3.5', result:'3:2'}],
  amount : 1000
}
//单双
{
  context : [{cat:'单双', odd:0.8, beton:'单', result:'3:2'}],
  amount : 1000
}
//过关, 每场比赛只能赌一注.
{
  cat:'过关',
  context : [{odd:0.8, beton:'单', cat:'单双', result:'3:2'}, {odd:0.8, cat:'大小',beton:'大', attach:'5', result:'3:2'}],
  amount : 1000
}
//滚球, 不完全相同于让球与大小, 主要是比分判断方面, 比分依据为最终比分减去当时比分.
{
  context : [{cat:'大小', odd:0.8, beton:'大', result:'1:0/3:2'}],
  amount : 1000
}
*/

onerror = handler;

function calc(abet)
{
  var bet = eval(abet);
  var ctx = bet.context;
  switch(bet.cat)
  {
  case "入球数" :
    if(ctx.beton == eval(ctx.result.replace(":", "+")))win(bet);
    else lose(bet);
  case "单双" :
    if(ctx.beton == (eval(ctx.result.replace(":", "+"))%2 ? "单" : "双"))win(bet);
    else lose(bet);
  case "波胆" :
    if(ctx.beton == ctx.result)win(bet);
    else lose(bet);
  case "独赢" :
    if(ctx.beton == calcr(ctx.result))win(bet);
    else lose(bet);
  case "半全场" :
    var r = ctx.result.split('/');
    var rr = calcr(r[0]) + "/" + calcr(r[1]);
    if(ctx.beton == rr)win(bet);
    else lose(bet);
  case "大小" :
    var sum = eval(ctx.result.replace(":", "+"));
    if(ctx.attach.search('/'))
    {
      var a = ctx.attach.split('/');
      ctx.s = calcr2(ctx.result, a[0], ctx.beton) + "/" + calcr2(ctx.result, a[1], ctx.beton);//possible result is 'draw/win'
    }
    else
      ctx.s = calcr2(ctx.result, ctx.attach, ctx.beton);
  case "让球" :
    var sum = eval(ctx.result.replace(":", "+"));
    //如果是主让客队...主队分数减去让球数.让球数分为2个, 通过'/'分隔的.
    //如果是客让主队...
  case "过关" :
  case "滚球" :
      
  }
}

function win(bet)
{
  alert("win ! ");
  var sum = bet.amount;
//  for(item in bet.context)sum *= item.odd;
}
function lose(bet)
{
  alert("lose ! ");
}
function draw(bet)
{
  
}

//str is '3:2', '4:0'..., the result is '主', '客' or '平'
function calcr(str)
{
    var home = eval(str.replace(":", ">"));
    var draw = eval(str.replace(":", "=="));
    return home ? "主" : draw ? "平" : "客";
}
//计算总入球数与基准的比较结果
function calcr2(str, base, beton)
{
  var sum = eval(str.replace(":", "+"));
  if(beton == sum) return "draw";
  else if(beton == (sum > base ? "大" : sum == base ? "平" : "小"))return "win";
  return "lose";
}




/////////////////////////////////////////////////////////////////////
//if error occurs, this function will handle it, so try java code if you want process it outside js code.
function handler(msg, url, line)
{
  println("赔率算法出现异常!!\n" + "msg : " + msg + "\nline : " + line);
  return msg + line;
}

//test codes......
var str = "abet = {\
  cat:'单双',\
  context : [{odd:0.8, beton:'单', result:'4:2'}],\
  amount : 1000\
}";
calc(str);